<template>
	<view class="content">
		
		<view class="login-form">
			<view class="uni-form-item uni-column po">
				<input class="uni-input" v-model="mobile" type="number" placeholder-style="color:#cacaca" placeholder="请输入手机号" />
				<view class="login-form-code-btn" @click="getCode">{{text}}</view>
			</view>
			<view class="uni-form-item uni-column po">
				<input class="uni-input" v-model="code" type="number" placeholder-style="color:#cacaca" placeholder="请输入手机验证码" />
			</view>
			<view class="uni-form-item uni-column po">
				<input class="uni-input" v-model="password" password type="text" placeholder-style="color:#cacaca" placeholder="请输入原登录密码" />
			</view>
			<view class="">
				<view class="login-form-btn" @click="post" :class="[disabled ? '' : 'disabled']">确认</view>				
			</view>
		</view>
	</view>
</template>

<script>
	import {
        mapState,
        mapMutations
    } from 'vuex'
	
	export default {
		computed: {
			...mapState(['token','version','SystemInfo']),
			text(){
				return this.time > 0 ? this.time + 's重新获取' : '获取验证码';
			}
		},
		data() {
			return {
				code: '',
				mobile: '',
				password: '',
				disabled:false,
				code_disabled: false,
				time: 0
			}
		},
		onNavigationBarButtonTap(e) {
			uni.navigateTo({
				url: '/pages/login/register/register'
			});
		},
		methods: {
			...mapMutations(['getNetworkType','vuexRequest','login','logout']),
			post() {
				if(this.mobile.length != 11){ 
					uni.showToast({
						icon: 'none',
						title: "请输入正确的手机号码",
						position:'bottom'
					})
					return false; 
				}
				if(!this.code){ 
					uni.showToast({
						icon: 'none',
						title: "请输入验证码",
						position:'bottom'
					})
					return false; 
				}
				if(!this.password){ 
					uni.showToast({
						icon: 'none',
						title: "请输入原登录密码",
						position:'bottom'
					})
					return false; 
				}
				uni.showLoading({
					title: '加载中'
				});
				
				// 默认信息
				let data = {mobile:this.mobile,code:this.code,password:this.password}
				
				this.vuexRequest({
					url: '/api/v2/member/member/change-mobile?access-token=' + this.token,
					method: 'POST',
					data: data,
					success: (res)  => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '修改成功',
							position:'bottom'
						})
						uni.navigateBack({
							
						})
					},
					fail: (err)  => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: err.data.message,
							position:'bottom'
						})
					},
				})
			},
			reset() {
				uni.navigateTo({
					url: '/pages/login/reset/reset'
				});
			},
			codeLogin() {
				uni.navigateTo({
					url: '/pages/login/code-login/code-login'
				});
			},
			protocol() {
				uni.navigateTo({
					url: '/pages/login/protocol/protocol'
				});
			},
			getCode() {
				if(this.mobile.length != 11){ 
					uni.showToast({
						icon: 'none',
						title: "请输入正确的手机号码",
						position:'bottom'
					})
					return false; 
				}
				uni.showLoading({
					title: '发送中'
				});
				this.vuexRequest({
					url: '/api/v2/member/member/code?access-token=' + this.token,
					method: 'POST',
					data: {usage:'change_mobile',mobile:this.mobile ,group:'app'},
					success: (res)  => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: res.data.message,
							position:'bottom'
						})
						this.time = 60;
						this.timer()
					},
					fail: (err)  => {
						uni.showToast({
							icon: 'none',
							title: err.data.message,
							position:'bottom'
						})
						uni.hideLoading()
					},
				})
			},
			timer() {
				if (this.time > 0) {
					this.time--;
					setTimeout(this.timer, 1000);
				}
		    }
		},
		watch: {
			moblie () {
				if(this.moblie != '' && this.password != '' && this.code != ''){
					this.disabled = true
				}else{
					this.disabled = false
				}
			},
			password(){
				if(this.moblie != '' && this.password != '' && this.code != ''){
					this.disabled = true
				}else{
					this.disabled = false
				}
			},
			code(){
				if(this.moblie != '' && this.password != '' && this.code != ''){
					this.disabled = true
				}else{
					this.disabled = false
				}
			}
		}
	}
</script>

<style>
	page{
		background: #FFFFFF;
	}
	.tips{
		text-align: center;
		font-size: 26upx;
		color: #e83e41;
		margin: 0 0 30upx 0 ;
	}
	.po{
		position: relative;
	}
	.po:after{
		position: absolute;
		z-index: 10;
		right: 0;
		bottom: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		-ms-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}
	.login-logo{
		text-align: center;
		padding: 60upx 0;
	}
	.login-logo-image{
		width: 153upx;
		height: 149upx;
		margin: 0 auto;
	}
	.login-form{
		width: 85%;
		margin: 0 auto;
	}
	.uni-input{
		padding-left: 0;
	}
	
	.login-form-checkbox {
		display: flex;
		padding:  20upx 0 0 0;
		font-size: 24upx;
		color: #cacaca;
		display: inline-flex;
	}
	uni-checkbox-group uni-label{
		padding: 0;
	}
	uni-radio-group, uni-checkbox-group{
		width: auto;
	}
	.login-form-link{
		color: #e83e41;
	}
	.login-form-btn{
		margin-top: 80upx;
		background: ;
		background-image: linear-gradient(to right, #f27072, #ea4a4c);
		text-align: center;
		border-radius: 200upx;
		height: 80upx;
		line-height: 80upx;
		font-size: 32upx;
		color: #FFFFFF;
	}
	.login-form-btn.disabled{
		background: #f09596;
	}
	.login-form-tips{
		padding-top: 40upx;
		text-align: center;
		color: #979797;
	}
	.login-form-code-btn{
		position: absolute;
		right: 0;
		top: 50%;
		color: #979797;
		margin-top: -30upx;
		font-size: 28upx;
		display: block;
		width: 180upx;
		height: 60upx;
		z-index: 10;
		line-height: 60upx;
		text-align: center;
	}
	.login-form-code-btn.disabled{
		color: #cacaca;
	}
</style>
